import java.util.HashMap;
import java.util.Map;

/**
 * Created by loso on 2017/3/17.
 */
public class _205_IsomorphicStrings {

    public static void main(String[] args) {
        _205_IsomorphicStrings a = new _205_IsomorphicStrings();
        System.out.println(a.isIsomorphic("egg","add"));
    }


    /**因为是要找模式是不是一样，但是只要是一一对应的字符的个数一样的话就是可以的，所以建立一个
     * char的表，然后记录对应的个数，如果不一样，说明模式一样不一样。
     */
    public boolean isIsomorphicBigboos(String s,String t){
        int[] mode1 = new int[256];
        int[] mode2 = new int[256];
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (mode1[s.charAt(i)] != mode2[t.charAt(i)]){
                return false;
            }
            mode1[s.charAt(i)]++;
            mode2[t.charAt(i)]++;
        }
        return true;
    }

    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()){
            return false;
        }
        int[] mode1 = getmode(s);
        int[] mode2 = getmode(t);
        return judgeMode(mode1,mode2);
    }

    public boolean judgeMode(int[] mode1, int[] mode2){
        for (int i = 0; i < mode1.length; i++) {
            if (mode1[i] != mode2[i]){
                return false;
            }
        }
        return true;
    }

    public int[] getmode(String s){
        int count = 0;
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))){
                map.put(s.charAt(i),count++);
            }
        }
        int[] req = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            req[i] = map.get(s.charAt(i));
        }
        return req;
    }

}
